﻿using HomagChina.Basic.CCT.PLC;
using HomagGroup.Base.Communication.Items;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Proxy
{
    public class OutfeedHandle
    {
        PlcHandle Handle_Plc;
        SearchInfoHandle Handle_Search;
        ModifyInfoHandle Handle_Modify;
        OutfeedTransportHandle Handle_OutfeedTransport;
        public OutfeedHandle()
        {
            Handle_Plc = new PlcHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_OutfeedTransport = new OutfeedTransportHandle();
            Task.Factory.StartNew(() => Position_Outfeed());
            Task.Factory.StartNew(() => Handle_OutfeedTransport.Position_Transport("70"));
        }


        private void Position_Outfeed()
        {
            do
            {
            Start:
                Thread.Sleep(1000);
                try
                {
                    if (Globle.isOutfeedActive)
                    {
                        var orderState = (int)(Handle_Plc.Read(PlcItems.Item_S100_P1_OrderState, "Int"));
                        if (!orderState.Equals((int)EnumOfOrderState.Plc请求数据)
                            && !orderState.Equals((int)EnumOfOrderState.Plc完成任务))//不存在请求和完成的状态下不处理
                        {
                            goto Start;
                        }

                        var Reference = Handle_Plc.Read(PlcItems.Item_S100_P1_In_Reference, "String").ToString();
                        SerilogHandle.Logger_P100.Information("Reference :" + Reference);
                        SerilogHandle.Logger_P100.Information("orderState :" + orderState);
                        switch (orderState)
                        {
                            case (int)EnumOfOrderState.Plc完成任务:
                            Write2Plc:
                                ResultCode WriteFlag = Handle_Plc.Write(PlcItems.Item_S100_P1_OrderState, (int)EnumOfOrderState.CCT完成任务);
                                if (WriteFlag != ResultCode.Success)
                                {
                                    goto Write2Plc;
                                }
                                Handle_Modify.ModifyTransState(Reference);
                                break;
                            case (int)EnumOfOrderState.Plc请求数据:
                                Handle_Plc.Write(PlcItems.Item_S100_P1_Out_Direction, (int)EnumOfDirection.Forward);
                                Handle_Plc.Write(PlcItems.Item_S100_P1_OrderState, (int)EnumOfOrderState.CCT下发任务);
                                break;
                            default:
                                break;
                        }
                        goto Start;
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_P100.Error(ex.Message);
                    goto Start;
                }
            } while (true);
        }
    }
}
